<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\FlowTimesConfig;
use App\Admin\Actions\Grid\ZengJianConfig1;
use App\Admin\Repositories\ChannelReport;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ChannelReportController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ChannelReport(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('channel_num');
            $grid->column('channel_name');
            $grid->column('today_install');
            $grid->column('active_reg');
            $grid->column('active_tourist');
            $grid->column('pv');
            $grid->column('uv');
            $grid->column('active_ip');
            $grid->column('oneday_keep');
            $grid->column('sevenday_keep');
            $grid->column('thirtyday_keep');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->toolsWithOutline(false);
            $grid->tools(new ZengJianConfig1());
            $grid->tools(new FlowTimesConfig());
            // 显示过滤器按钮
            $grid->showFilterButton();
            // 禁用新增
            $grid->disableCreateButton();
            $grid->filter(function($filter){
                $filter->panel();
                $filter->expand();
                $filter->where('day', function($query){
                    switch($this->input){
                        case 2: // 今天
                            $query->whereDate('created_at', date('Y-m-d'));
                            break;
                        case 3; // 昨天
                            $query->whereDate('created_at', date('Y-m-d', strtotime('yesterday')));
                            break;
                        case 4; // 最近7天
                            $query->whereBetween('created_at', [
                                date('Y-m-d', mktime(0,0,0,date('m'),date('d')-7,date('y'))),
                                date('Y-m-d'),
                            ]);
                            break;
                        case 5; // 最近30天
                            $query->whereBetween('created_at', [
                                date('Y-m-d', mktime(0,0,0,date('m'),date('d')-30,date('y'))),
                                date('Y-m-d'),
                            ]);
                            break;
                    }
                }, '最近多少天')->select([1 => '所有时间', 2 => '今天', 3 => '昨天', 4 => '最近7天', 5 => '最近30天'])->width(3)->default(1);
                $filter->between('created_at','自由选取')->date()->width(4);
                $filter->where('channel_name', function($query){
                    switch($this->input){
                        case 2: // android
                            $query->where('channel_name', 'like', "%android%")->orWhere('channel_name', 'like', "%安卓%");
                            break;
                        case 3: // IOS
                            $query->where('channel_name', 'like', "%IOS%")->orWhere('channel_name', 'like', "%ios%");
                            break;
                    }
                },'类型')->select([1 => 'all', 2 => 'android', 3 => 'IOS'])->width(3)->default(1);
            });
            Admin::script("
                $('input[name=\"created_at[start]\"]').blur(function(){
                    $('select[name=day]').val([1]).change();
                })
            ");
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ChannelReport(), function (Show $show) {
            $show->field('id');
            $show->field('channel_num');
            $show->field('channel_name');
            $show->field('today_install');
            $show->field('active_reg');
            $show->field('active_tourist');
            $show->field('pv');
            $show->field('uv');
            $show->field('active_ip');
            $show->field('oneday_keep');
            $show->field('sevenday_keep');
            $show->field('thirtyday_keep');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ChannelReport(), function (Form $form) {
            $form->display('id');
            $form->text('channel_num');
            $form->text('channel_name');
            $form->text('today_install');
            $form->text('active_reg');
            $form->text('active_tourist');
            $form->text('pv');
            $form->text('uv');
            $form->text('active_ip');
            $form->text('oneday_keep');
            $form->text('sevenday_keep');
            $form->text('thirtyday_keep');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
